package cn.itcast.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.itcast.dao.RoleDao;
import cn.itcast.dao.UserRoleDao;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.Role;
import cn.itcast.pojo.User;
import cn.itcast.service.RoleService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private UserRoleDao userRoleDao;

    /*
    * 根据关键字查询
    * */
    @Override
    public PageResult selectByNameRole(Integer currentPage, Integer pageSize, Long total, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        List<Role> roleList;
        if (StrUtil.isEmpty(queryString)){
            roleList = this.roleDao.selectRole();
        }else {
            roleList = this.roleDao.selectByNameRole(queryString);
        }
        return new PageResult(total,roleList);
    }

    /*
    * 查询所有角色
    * */
    @Override
    public List<Role> selectRoleAll() {
        return this.roleDao.selectRole();
    }

    /*
    * 根据id查询角色
    * */
    @Override
    public Role selectByIdRole(Integer id) {
        return this.roleDao.selectByIdRole(id);
    }

    /*
     * 查询角色关联的菜单ID
     * */
    @Override
    public Integer[] selectByIdRoleMenu(Integer id) {
        return this.roleDao.selectByIdRoleMenu(id);
    }

    /*
    * 查询角色关联的权限ID
    * */
    @Override
    public Integer[] selectByIdRolePermission(Integer id) {
        return this.roleDao.selectByIdRolePermission(id);
    }

    /*
    * 新增角色
    * */
    @Override
    public void saveRole(Role role, Integer[] permissionIds, Integer[] menuIds) {
        this.roleDao.saveRole(role);
        //往角色和权限中添加数据
        if (permissionIds.length != 0){
            this.setRolePermissionId(role.getId(),permissionIds);
        }
        //往角色和菜单中添加数据
        if (menuIds.length != 0){
            this.setRoleMenuId(role.getId(),menuIds);
        }
    }

    /*
    * 删除角色
    * */
    @Override
    public void deleteRole(Integer roleId) {
        this.roleDao.deleteRole(roleId);
        //将角色与权限中间表数据删除
        this.userRoleDao.deleteRolePermission(roleId);
        //将角色与菜单中间表数据删除
        this.userRoleDao.deleteRoleMenu(roleId);
        //将角色与菜单中间表数据删除
        this.userRoleDao.deleteRoleUser(roleId);
    }

    /*
    * 修改数据
    * */
    @Override
    public void updateUser(Role role, Integer[] permissionIds, Integer[] menuIds) {
        this.roleDao.updateRole(role);
        //将角色与权限中间表数据删除
        if (permissionIds != null){
            this.userRoleDao.deleteRolePermission(role.getId());
            //重新往角色和权限中添加数据
            this.setRolePermissionId(role.getId(),permissionIds);
        }
        //将角色与菜单中间表数据删除
        if (menuIds != null){
            this.userRoleDao.deleteRoleMenu(role.getId());
            //重新往角色和菜单中添加数据
            this.setRoleMenuId(role.getId(),menuIds);
        }

    }

    /*
     * 往角色和权限中添加数据
     * */
    private void setRolePermissionId(Integer roleId, Integer[] permissionIds) {
        for (Integer permissionId : permissionIds) {
            this.userRoleDao.saveRolePermission(roleId,permissionId);
        }
    }

    /*
     * 往角色和菜单中添加数据
     * */
    private void setRoleMenuId(Integer roleId, Integer[] menuIds) {
        for (Integer menuId : menuIds) {
            this.userRoleDao.saveRoleMenu(roleId,menuId);
        }
    }
}
